<html>
<head>
<meta name="robots" content="noindex,nofollow"/>
</head>
<body>
<?php
    if($_GET['action']==1):
    /**
	 * SEND EMAILS TO SUBSCRIBERS
	 */
	// EMAIL TO SUBCRIBER DAY BY DAY
	function cpx_email_daybyday($email_to, $arr_cp_ids, $st_id, $follow_id)
	{
		add_filter('wp_mail_content_type', 'set_html_content_type');
		//include('custom-script/email_templates/email_store.php');
		$st_name = get_post_field('post_title', $st_id);
		$title = "Today $st_name coupons";

		$email_content = cpx_get_email_store($arr_cp_ids, $st_id, $follow_id);
		wp_mail($email_to, $title, $email_content);
		remove_filter('wp_mail_content_type', 'set_html_content_type');
	}
	function set_html_content_type()
	{
		return 'text/html';
	}
	// GET TO DAY COUPONS
	function cpx_today_coupons_by_store($st_id)
	{
		global $wpdb;
		$today = date('Y-m-d', time());
		$qr = "
        SELECT ID FROM {$wpdb->posts} WHERE post_type = 'coupon' AND post_status = 'publish'
        AND DATE_FORMAT(post_date, '%Y-%m-%d') = '{$today}' AND ID IN
        (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='store_coupon_metadata' AND meta_value = {$st_id})
        ";
		$rs = $wpdb->get_results($qr);
		if (count($rs) > 0)
		{
			$arr = array();
			foreach ($rs as $r)
			{
				array_push($arr, $r->ID);
			}
			return $arr;
		}
		else
			return null;
	}
	// GET STORE HAVE COUPONS PUBLISHED TODAY
	function cpx_today_store_have_cp_published()
	{
		global $wpdb;
		$today = date('Y-m-d', time());
		$qr = "
        SELECT meta_value AS 'store_id' FROM {$wpdb->postmeta} WHERE meta_key = 'store_coupon_metadata' AND post_id IN
        ( SELECT ID FROM {$wpdb->posts} WHERE post_type = 'coupon' AND post_status = 'publish'
          AND DATE_FORMAT(post_date, '%Y-%m-%d') = '{$today}' ) GROUP BY meta_value;
        ";
		$rs = $wpdb->get_results($qr, ARRAY_N);
		return $rs;
	}
	// GET STORES BEING SUBSCRIBED
	function cpx_get_stores_subscribed()
	{
		global $wpdb;
		$qr = "
        SELECT VALUE FROM wp_follow GROUP BY VALUE
        ";
		$rs = $wpdb->get_results($qr, ARRAY_N);
		return $rs;
	}
	// GET EMAILS SUBCRIBED BY STORE
	function cpx_get_emails_subscribed_bystore($st_id)
	{
		global $wpdb;
		$qr = "SELECT user_email,follow_id FROM wp_follow WHERE VALUE = {$st_id}";
		$rs = $wpdb->get_results($qr);
		return $rs;
	}
	// ACTIVE ACTION SEND EMAIL TO SUBSCRIBER DAY BY DAY

	function cpx_doaction_sendemail_daybyday()
	{
		$arr_st_have_cps_published = cpx_today_store_have_cp_published();
		$arr_st_subscribed = cpx_get_stores_subscribed();
		include ("inc/custom-script/email_templates/email_store.php");
		foreach ($arr_st_have_cps_published as $store_id)
		{
			if (in_array($store_id, $arr_st_subscribed))
			{
				$arr_emails_following_this_store = cpx_get_emails_subscribed_bystore($store_id[0]);
				$cps_published_today = cpx_today_coupons_by_store($store_id[0]);
				foreach ($arr_emails_following_this_store as $e)
				{
					cpx_email_daybyday($e->user_email, $cps_published_today, $store_id[0], $e->follow_id);
					sleep(10);
				}
			}
		}
	}
    cpx_doaction_sendemail_daybyday();
    endif;
    ?>
 </body>
 </html>
